package com.example.mymusicdisplay.utils;

import android.util.Log;

import com.example.mymusicdisplay.data.lrc.LrcEntity;
import com.example.mymusicdisplay.data.music.RequestResult;
import com.example.mymusicdisplay.service.LrcService;

import org.reactivestreams.Publisher;

import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.functions.Function;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * author: ts
 * created on: 2021/10/25 下午4:17
 * description:
 */
public class LrcGetUtil {
    private static final String TAG = "LrcGetUtil";
    public static LrcEntity mLrcEntity = new LrcEntity();
    private static final Retrofit mRetrofit = new Retrofit.Builder()
        .baseUrl("https://autumnfish.cn/")
        .addConverterFactory(GsonConverterFactory.create()) //添加转换器
        .addCallAdapterFactory(RxJava3CallAdapterFactory.create()) //添加Rxjava3的适配器
        .build();
    private static final LrcService mLrcService = mRetrofit.create(LrcService.class);

    public static Flowable<Object> getLrc(String singName) {
        return mLrcService.getLrcUrl(singName)
            .flatMap(new Function<RequestResult, Publisher<?>>() {
                @Override
                public Publisher<?> apply(RequestResult requestResult) throws Throwable {
                    Log.d(TAG, "apply: Publisher");
                    if (requestResult.getCode() != 200) {
                        return null;
                    } else {
                        return mLrcService.getLrcData(requestResult.getResult().getSongs().get(0).getId());
                    }
                }
            });
    }
}
